<?php
 session_start();
 ?>
 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><!--DVID=00001DE9-->
<HTML>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<?
 if ($_POST['cancel']){
    header("location:con_registration.php");
 }
  //load database 
require_once('connections/mysqlDB.php');
// load Smarty library
require_once('smarty_connect.php');
$regid =$_SESSION['regid'];

$smarty = new smarty_connect;
 //connect to the database
$dbConn = new mysqli(HOST,USER,PWD,DB);
if (mysqli_connect_errno()) 
{ 
    printf("Can't connect to MySQL Server. Errorcode: %s\n", mysqli_connect_error()); 
    exit(); 
}
$dbConn->query("SET NAMES 'utf8'");
$stmt1 = $dbConn->query("SELECT abbrev, trim(description) as description from code where code = 'country' order by id");
$country_options=array();
 while ($row=$stmt1->fetch_array(MYSQL_NUM)) {
 $country_options[$row[0]]="$row[1]";
};
$stmt1->close();

$stmt1 = $dbConn->query("SELECT abbrev, trim(description) as description from code where code = 'state' order by id");
$state_options=array();
 while ($row=$stmt1->fetch_array(MYSQL_NUM)) {
 $state_options[$row[0]]="$row[1]";
};
$stmt1->close();

$stmt1 = $dbConn->query("SELECT abbrev, trim(description) as description from code where code = 'gender' order by id");
$gender_options=array();
 while ($row=$stmt1->fetch_array(MYSQL_NUM)) {
 $gender_options[$row[0]]="$row[1]";
};
$stmt1->close();

// get relationship  list
$stmt1 = $dbConn->query("SELECT cmid, church_name from church order by church_name");
$church_options=array();
while ($row=$stmt1->fetch_array(MYSQL_NUM)) {
  $church_options[$row[0]]="$row[1]";
};
  $church_options["0"]="Other";
$stmt1->close();

$smarty->assign('pastor_options', array('P'=>'牧師 Pastor','Y'=>'青少年牧師 Youth Pastor','M'=>'宣教士 Missionary','I'=>'傳道人 Minister'));
$smarty->assign('church_size_options', array('01'=>'50人以下','02'=>'50-100 人','03'=>'100-500 人','04'=>'500 人以上','05'=>'其他 Other '));


//set the current action
$_action=isset($_REQUEST['action'])? $_REQUEST['action'] : 'view';
 if ($_POST['update']){
    $_action='update';
 } 
 if ($_POST['delete']){
    $_action='delete';
 }
 switch($_action)
 {             
    case 'update':
    //collect value from the form field
    $cname_p=trim($_POST['cname']);
    $firstname_p=trim($_POST['firstname']);
    $lastname_p=trim($_POST['lastname']); 
    $homephone_p=trim($_POST['home_phone']); 
    $mobilephone_p=trim($_POST['mobile_phone']);
    $workphone_p=trim($_POST['work_phone']); 
    $gender_p=trim($_POST['gender']);
    $psf_p=trim($_POST['pastoral_staff']); 
    $title_p=trim($_POST['title']); 
    $email_p=trim($_POST['email']); 
    $pwd_p=trim($_POST['password']); 
    $address1_p=trim($_POST['address1']);
    $address2_p=trim($_POST['address2']); 
    $city_p=trim($_POST['city']); 
    $state_p=trim($_POST['state']); 
    $zip_p=trim($_POST['zipcode']);
    $country_p=trim($_POST['country']); 
	$cmid_p=trim($_POST['church']);
    $church_p=trim($_POST['other_church']);
    $church_size_p=trim($_POST['church_size']);
    $church_other_p=trim($_POST['church_size_other']);
       
    if(strlen($_POST['firstname']) == 0) {
            $error_firstname = "fname_empty";
    }
    if(strlen($_POST['lastname']) == 0) {
            $error_lastname = "lname_empty";
    }
    if(strlen($_POST['password']) == 0) {
            $error_pwd = "password_empty";
    }
    if(strlen($_POST['country']) == 0) {
            $error_country = "country_empty";
    }
    if(strlen($_POST['email']) == 0) {
            $error_email = "email_empty";
    }
    if(($error_firstname == "") && ($error_lastname == "") &&($error_country == "") && ($error_pwd == "") && ($error_email == ""))
    {  
       //update the record                
       $updsql="UPDATE registrant SET cname ='$cname_p', first_name ='$firstname_p', last_name='$lastname_p', home_phone='$homephone_p',mobile_phone='$mobilephone_p',work_phone='$workphone_p',gender='$gender_p', pastoral_staff='$psf_p', title='$title_p', email='$email_p', pwd='$pwd_p',address1='$address1_p', address2='$address2_p',city='$city_p', state='$state_p', zip='$zip_p', country='$country_p', cmid= $cmid_p, church='$church_p',church_size='$church_size_p',church_size_other='$church_other_p',last_update=NOW() WHERE regid=$regid";
       $dbConn->query($updsql);    
       //select the updated record and display them in the form
       $sql = "SELECT * FROM registrant WHERE regid= ?";
     if ($stmt = $dbConn->prepare($sql)) 
     {
        $stmt->bind_param("s", $regid);
        $stmt->execute();
        $stmt->bind_result($regid,$cmid,$cname,$firstname,$lastname,$homephone,$mobilephone,$workphone,$gender,$pastoral_staff,$title,$email,$pwd,$address1,$address2,$city,$state,$zipcode,$country,$status,$create_date,$church,$church_id,$last_update,$last_user,$church_size,$church_size_other,$user_level);
        $stmt->fetch();
        $stmt->close();
        $smarty->assign('cname', $cname);
        $smarty->assign('firstname', $firstname);
        $smarty->assign('lastname', $lastname);
        $smarty->assign('gender', $gender);
        $smarty->assign('pastoral_staff', $pastoral_staff);
        $smarty->assign('title', $title);
        $smarty->assign('email', $email);
        $smarty->assign('pwd', $pwd);
        $smarty->assign('homephone', $homephone);
        $smarty->assign('mobilephone', $mobilephone);
        $smarty->assign('workphone', $workphone);
        $smarty->assign('address1', $address1);
        $smarty->assign('address2', $address2);
        $smarty->assign('city', $city);
        $smarty->assign('state', $state);
        $smarty->assign('zipcode', $zipcode);
        $smarty->assign('country', $country);
        $smarty->assign('country_options',$country_options);
        $smarty->assign('state_options',$state_options);
        $smarty->assign('gender_options',$gender_options);
        $smarty->assign('church_options',$church_options);
        $smarty->assign('church', $cmid);
		$smarty->assign('other_church', $church);
        $smarty->assign('church_size',$church_size);
        $smarty->assign('error_email','');
        $smarty->assign('error_pwd','');
        $smarty->assign('error_firstname','');
        $smarty->assign('error_lastname','');
        $smarty->assign('error_country','');
        $smarty->assign('error','');
        $smarty->assign('church_size_other',$church_size_other);
        $smarty->assign('SCRIPT_NAME','con_registration.php');
        $smarty->display('con_updregistrant.tpl');
     }
    }
    else
    {
       $sql = "SELECT * FROM registrant WHERE regid= ?";
     if ($stmt = $dbConn->prepare($sql)) 
     {
        $stmt->bind_param("s", $regid);
        $stmt->execute();
        $stmt->bind_result($regid,$cmid,$cname,$firstname,$lastname,$homephone,$mobilephone,$workphone,$gender,$pastoral_staff,$title,$email,$pwd,$address1,$address2,$city,$state,$zipcode,$country,$status,$create_date,$church,$church_id,$last_update,$last_user,$church_size,$church_size_other,$user_level);
        $stmt->fetch();
        $stmt->close();
        $smarty->assign('cname', $cname);
        $smarty->assign('firstname', $firstname);
        $smarty->assign('lastname', $lastname);
        $smarty->assign('gender', $gender);
        $smarty->assign('pastoral_staff', $pastoral_staff);
        $smarty->assign('title', $title);
        $smarty->assign('email', $email);
        $smarty->assign('pwd', $pwd);
        $smarty->assign('homephone', $homephone);
        $smarty->assign('mobilephone', $mobilephone);
        $smarty->assign('workphone', $workphone);
        $smarty->assign('address1', $address1);
        $smarty->assign('address2', $address2);
        $smarty->assign('city', $city);
        $smarty->assign('state', $state);
        $smarty->assign('zipcode', $zipcode);
        $smarty->assign('country', $country);
        $smarty->assign('country_options',$country_options);
        $smarty->assign('state_options',$state_options);
        $smarty->assign('gender_options',$gender_options);
        $smarty->assign('church_options',$church_options);
        $smarty->assign('church', $cmid);
		$smarty->assign('other_church', $church);		
        $smarty->assign('church_size',$church_size);
        $smarty->assign('error_email',$error_email);
        $smarty->assign('error_pwd',$error_pwd);
        $smarty->assign('error_firstname',$error_firstname);
        $smarty->assign('error_lastname',$error_lastname);
        $smarty->assign('error_country',$error_country);
        $smarty->assign('error','Y');
        $smarty->assign('church_size_other',$church_size_other);
        $smarty->assign('SCRIPT_NAME','con_registrant.php');
        $smarty->display('con_registrant.tpl');
     }
    }
    break;
    case 'delete':
    $selsql =  "SELECT reginfoid FROM registration,registration_info WHERE regid=$regid AND registration.rid=registration_info.rid";
    $result=$dbConn->query($selsql);
    if($result->num_rows>=1)
    {
      $sql = "SELECT * FROM registrant WHERE regid= ?";
      if ($stmt = $dbConn->prepare($sql)) 
      {
        $stmt->bind_param("s", $regid);
        $stmt->execute();
        $stmt->bind_result($regid,$cmid,$cname,$firstname,$lastname,$homephone,$mobilephone,$workphone,$gender,$pastoral_staff,$title,$email,$pwd,$address1,$address2,$city,$state,$zipcode,$country,$status,$create_date,$church,$church_id,$last_update,$last_user,$church_size,$church_size_other,$user_level);
        $stmt->fetch();
        $stmt->close();
        $smarty->assign('cname', $cname);
        $smarty->assign('firstname', $firstname);
        $smarty->assign('lastname', $lastname);
        $smarty->assign('gender', $gender);
        $smarty->assign('pastoral_staff', $pastoral_staff);
        $smarty->assign('title', $title);
        $smarty->assign('email', $email);
        $smarty->assign('pwd', $pwd);
        $smarty->assign('homephone', $homephone);
        $smarty->assign('mobilephone', $mobilephone);
        $smarty->assign('workphone', $workphone);
        $smarty->assign('address1', $address1);
        $smarty->assign('address2', $address2);
        $smarty->assign('city', $city);
        $smarty->assign('state', $state);
        $smarty->assign('zipcode', $zipcode);
        $smarty->assign('country', $country);
        $smarty->assign('country_options',$country_options);
        $smarty->assign('state_options',$state_options);
        $smarty->assign('gender_options',$gender_options);
        $smarty->assign('church_options',$church_options);
        $smarty->assign('church', $cmid);
		$smarty->assign('other_church', $church);		
        $smarty->assign('church_size',$church_size);
        $smarty->assign('church_size_other',$church_size_other);
        $smarty->assign('SCRIPT_NAME','con_registration.php');
        $smarty->display('con_delregistrant.tpl');
     }
    }
    else{
        $delsql = "DELETE FROM registrant WHERE regid=$regid" ;
        $dbConn->query($delsql);
        $smarty->display('redirect.tpl');
    }
    break;
    case 'view':
    default:
     $sql = "SELECT * FROM registrant WHERE regid= ?";
     if ($stmt = $dbConn->prepare($sql)) 
     {
        $stmt->bind_param("s", $regid);
        $stmt->execute();
        $stmt->bind_result($regid,$cmid,$cname,$firstname,$lastname,$homephone,$mobilephone,$workphone,$gender,$pastoral_staff,$title,$email,$pwd,$address1,$address2,$city,$state,$zipcode,$country,$status,$create_date,$church,$church_id,$last_update,$last_user,$church_size,$church_size_other,$user_level);
        $stmt->fetch();
        $stmt->close();
        $smarty->assign('cname', $cname);
        $smarty->assign('firstname', $firstname);
        $smarty->assign('lastname', $lastname);
        $smarty->assign('gender', $gender);
        $smarty->assign('pastoral_staff', $pastoral_staff);
        $smarty->assign('title', $title);
        $smarty->assign('email', $email);
        $smarty->assign('pwd', $pwd);
        $smarty->assign('homephone', $homephone);
        $smarty->assign('mobilephone', $mobilephone);
        $smarty->assign('workphone', $workphone);
        $smarty->assign('address1', $address1);
        $smarty->assign('address2', $address2);
        $smarty->assign('city', $city);
        $smarty->assign('state', $state);
        $smarty->assign('zipcode', $zipcode);
        $smarty->assign('country', $country);
        $smarty->assign('country_options',$country_options);
        $smarty->assign('state_options',$state_options);
        $smarty->assign('gender_options',$gender_options);
        $smarty->assign('church_options',$church_options);
        $smarty->assign('church', $cmid);
		$smarty->assign('other_church', $church);		
        $smarty->assign('church_size',$church_size);
        $smarty->assign('church_size_other',$church_size_other);
        $smarty->assign('SCRIPT_NAME','con_registrant.php');
        $smarty->display('con_registrant.tpl');
     }
     break;
}
?>